package com.sangfor.ssl.easyapp;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.IConstants;
import com.sangfor.ssl.IVpnDelegate;
import com.sangfor.ssl.SFException;
import com.sangfor.ssl.SangforAuth;
import com.sangfor.ssl.SangforAuthManager;
import com.sangfor.ssl.TicketAuthManager;
import com.sangfor.ssl.common.DeviceIDManger;
import com.sangfor.ssl.common.ErrorCode;
import com.sangfor.ssl.common.Foreground;
import com.sangfor.ssl.common.SFError;
import com.sangfor.ssl.common.VpnCommon;
import com.sangfor.ssl.l3vpn.service.L3vpnController;
import com.sangfor.ssl.l3vpn.service.LogoutTaskState;
import com.sangfor.ssl.l3vpn.service.VpnServiceManager;
import com.sangfor.ssl.l3vpn.service.VpnStatus;
import com.sangfor.ssl.service.setting.ResourceManager;
import com.sangfor.ssl.service.setting.SettingManager;
import com.sangfor.ssl.service.setting.SystemConfiguration;
import java.lang.ref.WeakReference;
import java.net.URL;

/* loaded from: classes2.dex */
public class SangforAuthForward implements IConstants {
    public static final int MSG_AUTH_RESULT_CALLBACK = 1;
    public static final int MSG_L3VPN_FATAL = 7;
    public static final int MSG_L3VPN_RELOGIN = 6;
    public static final int MSG_L3VPN_RESULT_CALLBACK = 2;
    public static final int MSG_RELOGIN_RESULR_CALLBACK = 4;
    public static final int MSG_RND_CODE_CALLBACK = 3;
    public static final int MSG_STATUS_CHANGE_CALLBACK = 8;
    public static final int MSG_VPN_STATUS_CALL_BACK = 9;
    private static final int RESULT_OTHER_RELOGIN_FAIL = 0;
    private static final int RESULT_OTHER_RELOGIN_NETWORK_ERROR = 2;
    private static final int RESULT_OTHER_RELOGIN_SUCCESS = 1;
    private static final String TAG = "SangforAuthForward";
    protected static SangforAuthForward instance = null;
    protected static Context mContext = null;
    protected static Handler mHandler = null;
    protected static boolean sL3vpnMode = true;
    protected int mL3vpnLocalStatus = 0;
    protected WeakReference<IVpnDelegate> mDelegate = null;

    private native int hookAndroidJniMethods(int i);

    private native String nGetPastAuthEmm();

    protected static native int nGetProxyPort();

    private native int nSetCrptFileRule(String str);

    private native int nSetExCrptFileRule(String str);

    private native int sangforHook(int i);

    /* JADX INFO: Access modifiers changed from: protected */
    public native String getCertSubject(String str, String str2);

    /* JADX INFO: Access modifiers changed from: protected */
    public native String nChangePassword(String str, String str2);

    /* JADX INFO: Access modifiers changed from: protected */
    public native void nCleanSession();

    /* JADX INFO: Access modifiers changed from: protected */
    public native void nClearAuthProperties();

    /* JADX INFO: Access modifiers changed from: protected */
    public native String nGetChallengeMessage();

    /* JADX INFO: Access modifiers changed from: protected */
    public native String nGetLocalRclist(boolean z);

    protected native String nGetLoginAuthConf(String str);

    /* JADX INFO: Access modifiers changed from: protected */
    public native String nGetPasswordPolicy();

    public native String nGetResultProperty(String str);

    /* JADX INFO: Access modifiers changed from: protected */
    public native String nGetSmsCountdown();

    /* JADX INFO: Access modifiers changed from: protected */
    public native String nGetSmsPhoneNum();

    protected native String nGetTicketInfo(String str);

    public native String nGetTwfid();

    /* JADX INFO: Access modifiers changed from: protected */
    public native int nRegetSmsCode();

    /* JADX INFO: Access modifiers changed from: protected */
    public native boolean nSetDnsWhiteRule(String str);

    protected native int nSetLoginAuthConf(String str, String str2);

    /* JADX INFO: Access modifiers changed from: protected */
    public native int nSetLoginParam(String str, String str2, boolean z);

    protected native void nSetTcpMode();

    public native void nSetTwfid(String str);

    protected native int nStartForward();

    protected native void nUpdateVpnInfo(String str, long j, int i);

    /* JADX INFO: Access modifiers changed from: protected */
    public native int nVpnCancelLogin(boolean z);

    protected native String nVpnGetAuthMessage();

    protected native int nVpnGetErrorCode();

    protected native int nVpnGetLocalStatus();

    /* JADX INFO: Access modifiers changed from: protected */
    public native int nVpnGetRndCode(boolean z);

    /* JADX INFO: Access modifiers changed from: protected */
    public native String nVpnGetVersion(boolean z);

    protected native String nVpnGeterr(boolean z);

    protected native int nVpnInit(String str, long j, int i, int i2, boolean z);

    /* JADX INFO: Access modifiers changed from: protected */
    public native int nVpnLogin(int i, boolean z);

    public native int nVpnLogout(boolean z);

    /* JADX INFO: Access modifiers changed from: protected */
    public native String nVpnQueryHardID(boolean z);

    protected native int nVpnQuerySessionStatus(boolean z);

    protected native int nVpnQueryStatus(boolean z);

    /* JADX INFO: Access modifiers changed from: protected */
    public native int nVpnQuit(boolean z);

    protected native int nVpnRelogin(boolean z);

    protected native void nVpnSetErrorCode(int i, boolean z);

    /* JADX INFO: Access modifiers changed from: protected */
    public native void nVpnSeterr(String str, boolean z);

    /* JADX INFO: Access modifiers changed from: protected */
    public native int setMobileId(String str);

    static /* synthetic */ boolean access$100() {
        return startL3VPNService();
    }

    public static SangforAuthForward getInstance() {
        SangforAuthForward sangforAuthForward;
        synchronized (SangforAuthForward.class) {
            if (instance == null) {
                instance = new SangforAuthForward();
            }
            sangforAuthForward = instance;
        }
        return sangforAuthForward;
    }

    public void setL3vpnLocalStatus(int i) {
        this.mL3vpnLocalStatus = i;
    }

    public void init(Application application, Context context, IVpnDelegate iVpnDelegate, int i) throws SFException {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            throw new SFException("current thread is not main thread.");
        }
        if (iVpnDelegate == null) {
            throw new SFException("param error, delegate is null.");
        }
        mContext = application.getApplicationContext();
        mHandler = new CallbackHandler(Looper.getMainLooper());
        DeviceIDManger.getInstance().initDeviceId(mContext);
        DeviceIDManger.getInstance().stroeDeviceId();
        Foreground.init(application);
        setDelegate(iVpnDelegate);
        if (i == 1) {
            sL3vpnMode = false;
            sangforHook(Build.VERSION.SDK_INT);
        } else {
            sL3vpnMode = true;
            destoryObject();
        }
        L3vpnController.getInstance().register((Activity) context, mHandler);
    }

    public void destoryObject() {
        SystemConfiguration.destoryObject();
        SettingManager.destoryObject();
        ResourceManager.destoryObject();
        SystemConfiguration.isAbleWork = true;
        SettingManager.isAbleWork = true;
        SystemConfiguration.isAbleWork = true;
    }

    public boolean setDelegate(IVpnDelegate iVpnDelegate) {
        if (iVpnDelegate == null) {
            return false;
        }
        synchronized (this) {
            this.mDelegate = new WeakReference<>(iVpnDelegate);
        }
        return true;
    }

    public IVpnDelegate getDelegate() {
        IVpnDelegate iVpnDelegate;
        if (this.mDelegate != null) {
            synchronized (this) {
                iVpnDelegate = this.mDelegate.get();
            }
            return iVpnDelegate;
        }
        return null;
    }

    public boolean vpnInit(long j, int i, boolean z) {
        boolean z2 = sL3vpnMode;
        String str = (String) SettingManager.getInstance().getGlobalValue(SettingManager.GLOBAL_HOST_DOMAIN_KEY);
        try {
            URL url = new URL(str);
            str = url.getHost() + ":" + url.getPort();
        } catch (Exception e) {
            Log.info(TAG, "vpnInit", e);
        }
        return nVpnInit(str, j, i, z2 ? 1 : 0, z) == 0;
    }

    public void updateVpnInfo(long j, int i) {
        String str = (String) SettingManager.getInstance().getGlobalValue(SettingManager.GLOBAL_HOST_DOMAIN_KEY);
        try {
            URL url = new URL(str);
            str = url.getHost() + ":" + url.getPort();
        } catch (Exception e) {
            Log.info(TAG, "updateVpnInfo", e);
        }
        nUpdateVpnInfo(str, j, i);
    }

    public boolean setLoginParam(String str, String str2) {
        return nSetLoginParam(str, str2, false) == 0;
    }

    public boolean setLoginAuthConf(String str, String str2) {
        return nSetLoginAuthConf(str, str2) == 0;
    }

    public String getLoginAuthConf(String str) {
        return nGetLoginAuthConf(str);
    }

    public boolean vpnCancelLogin() {
        return nVpnCancelLogin(false) == 0;
    }

    public int queryVPNLocalStatus() {
        int nVpnGetLocalStatus = nVpnGetLocalStatus();
        return (sL3vpnMode && nVpnGetLocalStatus == 5) ? this.mL3vpnLocalStatus : nVpnGetLocalStatus;
    }

    public boolean vpnLogout() {
        if (sL3vpnMode) {
            Log.info(TAG, "start l3vpn logout");
            L3vpnController.getInstance().svpnLogout(LogoutTaskState.LogoutType.ACTIVE_LOGOUT);
            return true;
        }
        Log.info(TAG, "start easyapp logout");
        return nVpnLogout(true) == 0;
    }

    public boolean vpnQuit() {
        mContext = null;
        this.mDelegate = null;
        mHandler = null;
        return vpnRest();
    }

    public boolean vpnRest() {
        return nVpnQuit(false) == 0;
    }

    public String vpnQueryHardID() {
        Log.warn(TAG, "=====================vpnQueryHardID======================");
        return nVpnQueryHardID(false);
    }

    public int vpnGetRndCode() {
        return nVpnGetRndCode(false);
    }

    public String vpnGeterr() {
        return nVpnGeterr(false);
    }

    public int vpnGetErrorCode() {
        return nVpnGetErrorCode();
    }

    public String vpnGetVersion() {
        return nVpnGetVersion(false);
    }

    private static void vpnCallback(int i, int i2) {
        Handler handler = mHandler;
        if (handler != null) {
            Message obtain = Message.obtain();
            if (i == 7) {
                obtain.what = 8;
            } else if (i == 6) {
                obtain.what = 4;
            } else {
                obtain.what = 1;
                if (sL3vpnMode && i == 2 && i2 == 17 && !dealWithL3vpn()) {
                    i = -3;
                }
            }
            obtain.arg1 = i;
            obtain.arg2 = i2;
            handler.sendMessage(obtain);
            return;
        }
        Log.error(TAG, "handler is null, send login message failed.");
        throw null;
    }

    public static void vpnStatusCallback(int i) {
        Handler handler = mHandler;
        if (handler == null) {
            throw new NullPointerException("handler is null, vpnTunnelStatusCallback failed.");
        }
        Message obtain = Message.obtain();
        obtain.what = 9;
        obtain.arg1 = i;
        handler.sendMessage(obtain);
    }

    public void onActivityResult(int i, int i2) {
        IVpnDelegate iVpnDelegate;
        synchronized (this) {
            iVpnDelegate = this.mDelegate.get();
        }
        if (iVpnDelegate == null) {
            Log.info(TAG, "onActivityResult WeakReference get null");
        }
        if (i != 1) {
            return;
        }
        if (i2 == -1) {
            Log.info(TAG, "To grant the right to use VpnService success");
            this.mL3vpnLocalStatus = 5;
            L3vpnController.getInstance().startVPNBusiness();
            if (iVpnDelegate != null) {
                iVpnDelegate.vpnCallback(2, 17);
                return;
            }
            return;
        }
        this.mL3vpnLocalStatus = 2;
        Log.info(TAG, "To grant the right to use VpnService failed， logout...");
        L3vpnController.getInstance().svpnLogout(LogoutTaskState.LogoutType.ACTIVE_LOGOUT);
        if (iVpnDelegate != null) {
            iVpnDelegate.vpnCallback(-3, 17);
        }
    }

    private static boolean dealWithL3vpn() {
        String str = TAG;
        Log.info(str, "vpn login success in l3vpn mode");
        boolean initL3VPNService = L3vpnController.getInstance().initL3VPNService();
        if (!initL3VPNService) {
            Log.info(str, "init l3vpn info failed");
        } else if (!ResourceManager.getInstance().containL3vpnRc()) {
            initL3VPNService = startTcpService();
        } else {
            initL3VPNService = startL3VPNService();
            if (!initL3VPNService) {
                Log.info(str, "start l3vpn service failed");
            }
        }
        if (!initL3VPNService) {
            getInstance().nVpnSetErrorCode(ErrorCode.SF_ERROR_START_L3VPN_FAILED.value(), false);
            getInstance().nVpnSeterr(SFError.getErrorDesc(ErrorCode.SF_ERROR_START_L3VPN_FAILED), false);
        }
        return initL3VPNService;
    }

    private static boolean startTcpService() {
        Log.error(TAG, "startTcpService");
        sL3vpnMode = false;
        SangforAuth.getInstance().changeVpnMode(1);
        SangforAuthManager.getInstance().changeVpnMode(1);
        getInstance().nSetTcpMode();
        getInstance().sangforHook(Build.VERSION.SDK_INT);
        return getInstance().nStartForward() == 0;
    }

    private static boolean startL3VPNService() {
        String str = TAG;
        Log.error(str, "startL3VPNService");
        if (mContext == null) {
            Log.info(str, "context is null, start l3vpn failed");
            return false;
        }
        return L3vpnController.getInstance().startVPNBusiness();
    }

    private static void reloginCallback(int i, int i2) {
        Handler handler = mHandler;
        if (handler != null) {
            Message obtain = Message.obtain();
            obtain.what = 4;
            obtain.arg1 = i;
            obtain.arg2 = i2;
            handler.sendMessage(obtain);
            return;
        }
        Log.error(TAG, "handler is null, send relogin message failed.");
    }

    private static void vpnRndCodeCallback(byte[] bArr) {
        Handler handler = mHandler;
        if (handler != null) {
            Message obtain = Message.obtain();
            obtain.what = 3;
            obtain.obj = bArr;
            handler.sendMessage(obtain);
            return;
        }
        Log.error(TAG, "handler is null, send message failed.");
    }

    public String getTicketInfo(String str) {
        return nGetTicketInfo(str);
    }

    public void vpnRegetSmsCode() {
        new Thread() { // from class: com.sangfor.ssl.easyapp.SangforAuthForward.1
            @Override // java.lang.Thread, java.lang.Runnable
            public void run() {
                int i;
                int nRegetSmsCode = SangforAuthForward.this.nRegetSmsCode();
                synchronized (SangforAuthForward.class) {
                    if (SangforAuthForward.mHandler == null) {
                        Log.error(SangforAuthForward.TAG, "handler is null, send message failed.");
                        return;
                    }
                    int i2 = 2;
                    if (nRegetSmsCode == 0) {
                        i = 2;
                    } else {
                        i2 = -1;
                        i = 3;
                    }
                    SangforAuthForward.mHandler.obtainMessage(1, i2, i).sendToTarget();
                }
            }
        }.start();
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public class CallbackHandler extends Handler {
        private final String TAG;

        public CallbackHandler(Looper looper) {
            super(looper);
            this.TAG = "CallbackHandler";
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            IVpnDelegate iVpnDelegate;
            if (SangforAuthForward.this.mDelegate != null) {
                synchronized (this) {
                    iVpnDelegate = SangforAuthForward.this.mDelegate.get();
                }
            } else {
                iVpnDelegate = null;
            }
            if (iVpnDelegate == null) {
                Log.info("CallbackHandler", "handleMessage WeakReference get null");
            }
            switch (message.what) {
                case 1:
                    Log.info("CallbackHandler", "vpn result is " + message.arg1 + ", authType is " + message.arg2);
                    if (iVpnDelegate != null) {
                        iVpnDelegate.vpnCallback(message.arg1, message.arg2);
                        return;
                    }
                    return;
                case 2:
                    int i = message.arg1;
                    if (i == -6) {
                        SangforAuthForward.this.mL3vpnLocalStatus = 0;
                        message.arg1 = -3;
                        SangforAuthForward.this.vpnLogout();
                    } else if (i != 2) {
                        if (i == -4) {
                            SangforAuthForward.this.mL3vpnLocalStatus = 5;
                            SangforAuthForward.this.vpnLogout();
                        } else if (i == -3) {
                            SangforAuthForward.this.mL3vpnLocalStatus = 5;
                        }
                    } else if (message.arg2 == 100) {
                        SangforAuthForward.this.mL3vpnLocalStatus = 100;
                    }
                    if (iVpnDelegate != null) {
                        iVpnDelegate.vpnCallback(message.arg1, message.arg2);
                        return;
                    }
                    return;
                case 3:
                    Log.debug("CallbackHandler", "vpn randcode callback, data is " + message.obj);
                    if (iVpnDelegate != null) {
                        iVpnDelegate.vpnRndCodeCallback((byte[]) message.obj);
                        return;
                    }
                    return;
                case 4:
                    int i2 = message.arg2;
                    if (i2 == 0) {
                        Log.info("CallbackHandler", "reLogin failed!");
                        SangforAuthForward.this.vpnLogout();
                        return;
                    } else if (i2 != 1) {
                        if (i2 != 2) {
                            return;
                        }
                        Log.info("CallbackHandler", "reLogin have network error, wait for next reLogin!");
                        if (SangforAuthForward.sL3vpnMode) {
                            VpnServiceManager.getInstance().setShutDownStatus(true);
                            return;
                        }
                        return;
                    } else {
                        if (SangforAuthForward.sL3vpnMode) {
                            Log.info("CallbackHandler", "reLogin success, begin init l3vpn service!");
                            if (L3vpnController.getInstance().initL3VPNService()) {
                                Log.info("CallbackHandler", "init l3vpn service success");
                                SangforAuthForward.access$100();
                            } else {
                                Log.info("CallbackHandler", "reLogin success, but init l3vpn failed!");
                                return;
                            }
                        } else {
                            Log.info("CallbackHandler", "reLogin success!");
                        }
                        TicketAuthManager.getInstance().saveTicketInfo(true);
                        return;
                    }
                case 5:
                default:
                    return;
                case 6:
                    if (!((Boolean) SettingManager.getInstance().getGlobalValue(SettingManager.GLOBAL_AUTO_LOGIN_OFF, false)).booleanValue()) {
                        SangforAuthForward.this.nVpnRelogin(true);
                        return;
                    } else {
                        SangforAuthForward.this.vpnLogout();
                        return;
                    }
                case 7:
                    if (iVpnDelegate != null) {
                        iVpnDelegate.vpnCallback(-4, -1);
                        return;
                    }
                    return;
                case 8:
                    SangforAuthManager.getInstance().statusChangedCallback(message.arg2);
                    return;
                case 9:
                    if (iVpnDelegate != null) {
                        iVpnDelegate.vpnStatusCallback(VpnStatus.ValueOf(message.arg1));
                        return;
                    }
                    return;
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public String vpnGetHardwareId() {
        String str = (("" + VpnCommon.getHardwareIdByContext(mContext)) + "|" + VpnCommon.getMacAddressByContext(mContext)) + "|" + VpnCommon.getModelByContext(mContext);
        Log.info(TAG, "hardid: " + str);
        return str;
    }

    public String vpnGetPastAuthEmm() {
        return nGetPastAuthEmm();
    }

    public Context getContext() {
        return mContext;
    }
}
